<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>【Java学习】Java入门笔记1 | Jimmy's TechBlog</title><meta name="keywords" content="笔记"><meta name="author" content="哲铭"><meta name="copyright" content="哲铭"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Java学习笔记（1）1.1 什么是JDK、JRE、JVMJVM(Java Virtual Machine): Java虚拟机，简称JVM，是运行所有Java程序的假象计算机，是Java运行环境，同时也是Java最具吸引力的特征之一。我们编写的Java代码都将运行在JVM之上。 JRE(Java Runtime Environment):是Java程序的运行环境，包含JVM和运行时所需要的核心类库">
<!-- hexo-inject:begin --><!-- hexo-inject:end --><meta property="og:type" content="article">
<meta property="og:title" content="【Java学习】Java入门笔记1">
<meta property="og:url" content="https://jimmyliang-lzm.github.io/2021/02/09/Java-Note-01/index.html">
<meta property="og:site_name" content="Jimmy&#39;s TechBlog">
<meta property="og:description" content="Java学习笔记（1）1.1 什么是JDK、JRE、JVMJVM(Java Virtual Machine): Java虚拟机，简称JVM，是运行所有Java程序的假象计算机，是Java运行环境，同时也是Java最具吸引力的特征之一。我们编写的Java代码都将运行在JVM之上。 JRE(Java Runtime Environment):是Java程序的运行环境，包含JVM和运行时所需要的核心类库">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://jimmyliang-lzm.github.io/source_storage/java.jpg">
<meta property="article:published_time" content="2021-02-10T04:06:00.000Z">
<meta property="article:modified_time" content="2021-08-31T09:07:44.931Z">
<meta property="article:author" content="哲铭">
<meta property="article:tag" content="笔记">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://jimmyliang-lzm.github.io/source_storage/java.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://jimmyliang-lzm.github.io/2021/02/09/Java-Note-01/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//hm.baidu.com"/><link rel="preconnect" href="//fonts.googleapis.com" crossorigin=""/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?fec3351aac342e4baac87e889e540445";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Titillium+Web&amp;display=swap" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '【Java学习】Java入门笔记1',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-08-31 17:07:44'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><!-- hexo injector head_end start -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-math@4.0.0/dist/style.css">
<!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.3.0"><link rel="alternate" href="/atom.xml" title="Jimmy's TechBlog" type="application/atom+xml"><!-- hexo-inject:begin --><!-- hexo-inject:end -->
</head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">10</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">22</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">5</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页君</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间之线</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类君</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 今日最爱</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/%E9%9F%B3%E4%B9%90"><i class="fa-fw /music/"></i><span> 0</span></a></li><li><a class="site-page child" href="/%E7%85%A7%E7%89%87"><i class="fa-fw /gallery/"></i><span> 1</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 我♂</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/source_storage/java.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">Jimmy's TechBlog</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页君</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间之线</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类君</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 今日最爱</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/%E9%9F%B3%E4%B9%90"><i class="fa-fw /music/"></i><span> 0</span></a></li><li><a class="site-page child" href="/%E7%85%A7%E7%89%87"><i class="fa-fw /gallery/"></i><span> 1</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 我♂</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">【Java学习】Java入门笔记1</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-02-10T04:06:00.000Z" title="发表于 2021-02-10 12:06:00">2021-02-10</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-08-31T09:07:44.931Z" title="更新于 2021-08-31 17:07:44">2021-08-31</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Java%E5%AD%A6%E4%B9%A0/">Java学习</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="【Java学习】Java入门笔记1"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Java学习笔记（1）"><a href="#Java学习笔记（1）" class="headerlink" title="Java学习笔记（1）"></a>Java学习笔记（1）</h1><h2 id="1-1-什么是JDK、JRE、JVM"><a href="#1-1-什么是JDK、JRE、JVM" class="headerlink" title="1.1 什么是JDK、JRE、JVM"></a>1.1 什么是JDK、JRE、JVM</h2><p><strong>JVM</strong>(Java Virtual Machine): Java虚拟机，简称JVM，是运行所有Java程序的假象计算机，是Java运行环境，同时也是Java最具吸引力的特征之一。<strong>我们编写的Java代码都将运行在JVM之上</strong>。</p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><p><strong>JRE</strong>(Java Runtime Environment):是Java程序的运行环境，包含<code>JVM</code>和运行时所需要的<strong>核心类库</strong>。</p>
<p><strong>JDK</strong>(Java Development Kit):是Java程序开发工具包，包含<code>JRE</code>和开发人员使用的工具。</p>
<p><strong>我们想要运行一个已有的Java程序，那么只需要安装<code>JRE</code>即可。</strong></p>
<p><strong>我们想要开发一个全新的Java程序，那么必须安装<code>JDK</code>。</strong></p>
<h2 id="1-2-环境配置"><a href="#1-2-环境配置" class="headerlink" title="1.2 环境配置"></a>1.2 环境配置</h2><p><strong>JDK下载地址：</strong><a target="_blank" rel="noopener" href="https://www.oracle.com/java/technologies/javase-downloads.html">官方下载</a><br>安装即可</p>
<h2 id="1-3-第一个Java程序：HelloWorld"><a href="#1-3-第一个Java程序：HelloWorld" class="headerlink" title="1.3 第一个Java程序：HelloWorld"></a>1.3 第一个Java程序：HelloWorld</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HelloWorld</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Hello, World!&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>配置好环境变量后，可以通过打开<strong>CMD</strong>进行编译，在Java源代码<code>HelloWorld.java</code>根目录下运行代码：</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">javac HelloWorld.java</span><br></pre></td></tr></table></figure>
<p>将生成一个Java类文件<code>HelloWorld.class</code>，这就是Java的编译文件，随后通过运行命令提示符代码：</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java HelloWorld</span><br></pre></td></tr></table></figure>
<p>即可看到程序输出的<code>Hello, World!</code>字段。</p>
<h2 id="1-4-Java程序的注释"><a href="#1-4-Java程序的注释" class="headerlink" title="1.4 Java程序的注释"></a>1.4 Java程序的注释</h2><p>与C语言注释一致，具体可参照C语言注释方法，在此不再赘述。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 第一行的HelloWorld必须与其文件名一致，区分大小写</span></span><br><span class="line"><span class="comment">// public class后面代表定义一个类的名称，类是Java当中所有源代码的基本组织单位。</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HelloWorld</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 第二行的内容是万年不变的main方法</span></span><br><span class="line">    <span class="comment">// 这一行代表程序执行起点</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">    <span class="comment">/* 对系统输出字符</span></span><br><span class="line"><span class="comment">    Hello, World!</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">        System.out.println(<span class="string">&quot;Hello, World!&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="1-5-标识符"><a href="#1-5-标识符" class="headerlink" title="1.5 标识符"></a>1.5 标识符</h2><p>其命名规则与C语言基本相同，可包含<strong>26个大小写字母、0-9数字、美元符号<code>$</code>和下划线。</strong></p>
<h2 id="1-6-Java常量"><a href="#1-6-Java常量" class="headerlink" title="1.6 Java常量"></a>1.6 Java常量</h2><p><strong>常量分类：</strong></p>
<ol>
<li>字符型常量：凡是用双引号引起来的部分，叫做字符串常量。例如：”abc”、”Hello”、”123”；</li>
<li>整数常量：直接写上的数字，没有小数点。例如：100、200、0、-250；</li>
<li>浮点数常量：直接写上的数字，有小数点。例如：2.5、-3.14、0.0；</li>
<li>字符常量：凡是用单引号引起来的单个字符。例如：’A’、’b’、’9’、’中’；</li>
<li>布尔常量：只有两种取值。true、false；</li>
<li>空常量：代表没有任何数据，用null表示。</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo01Const</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">        <span class="comment">// 字符串常量</span></span><br><span class="line">        System.out.println(<span class="string">&quot;ABC&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;&quot;</span>); <span class="comment">//字符串两个双引号中间内容为空</span></span><br><span class="line">        System.out.println(<span class="string">&quot;XYZ&quot;</span>);</span><br><span class="line">        </span><br><span class="line">        <span class="comment">// 整数常量</span></span><br><span class="line">        System.out.println(<span class="number">30</span>);</span><br><span class="line">        System.out.println(-<span class="number">500</span>);</span><br><span class="line">        </span><br><span class="line">        <span class="comment">// 字符常量</span></span><br><span class="line">        System.out.println(<span class="string">&#x27;A&#x27;</span>);</span><br><span class="line">        System.out.println(<span class="string">&#x27;6&#x27;</span>);</span><br><span class="line">        <span class="comment">//System.out.println(&#x27;&#x27;); </span></span><br><span class="line">        <span class="comment">//System.out.println(&#x27;AB&#x27;);//两个单引号中间必有且仅有一个字符，没有不行，两个也不行！</span></span><br><span class="line">        </span><br><span class="line">        <span class="comment">//布尔常量</span></span><br><span class="line">        System.out.println(<span class="keyword">true</span>);</span><br><span class="line">        System.out.println(<span class="keyword">false</span>);</span><br><span class="line">        </span><br><span class="line">        <span class="comment">//空常量，不能直接用来打印输出</span></span><br><span class="line">        <span class="comment">//System.out.println(null);</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="1-7-基本数据类型"><a href="#1-7-基本数据类型" class="headerlink" title="1.7 基本数据类型"></a>1.7 基本数据类型</h2><p>Java的数据类型可以分为以下两大类：</p>
<ul>
<li><strong>基本数据类型：</strong> 包括<code>整数</code>、<code>浮点数</code>、<code>字符</code>、<code>布尔</code>。</li>
<li><strong>引用数据类型：</strong> 包括<code>类</code>、<code>数组</code>、<code>接口</code>。</li>
</ul>
<p>四类八种数据类型如下表所示：</p>
<table>
<thead>
<tr>
<th align="left">数据类型</th>
<th align="left">关键字</th>
<th align="left">内存占用</th>
<th align="left">取值范围</th>
</tr>
</thead>
<tbody><tr>
<td align="left">字节型</td>
<td align="left">byte</td>
<td align="left">1个字节</td>
<td align="left">-128~127</td>
</tr>
<tr>
<td align="left">短整型</td>
<td align="left">short</td>
<td align="left">2个字节</td>
<td align="left">-32768~32767</td>
</tr>
<tr>
<td align="left">整型</td>
<td align="left">int(Default)</td>
<td align="left">4个字节</td>
<td align="left">-2的31次方到2的31次方-1</td>
</tr>
<tr>
<td align="left">长整型</td>
<td align="left">long</td>
<td align="left">8个字节</td>
<td align="left">-2的63次方到2的63次方-1</td>
</tr>
<tr>
<td align="left">单精度浮点数</td>
<td align="left">float</td>
<td align="left">4个字节</td>
<td align="left">1.4013E-45~3.4028E+38</td>
</tr>
<tr>
<td align="left">双精度浮点数</td>
<td align="left">double(Default)</td>
<td align="left">8个字节</td>
<td align="left">4.9E-324~1.7977E+308</td>
</tr>
<tr>
<td align="left">字符型</td>
<td align="left">char</td>
<td align="left">2个字节</td>
<td align="left">0~65535</td>
</tr>
<tr>
<td align="left">布尔型</td>
<td align="left">boolean</td>
<td align="left">1个字节</td>
<td align="left">true false</td>
</tr>
</tbody></table>
<p>Java中的默认类型：整数类型是<code>int</code>、浮点类型是<code>double</code>。以下代码则进行测试</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">变量类型测试Demo</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo02Varible</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 创建一个变量</span></span><br><span class="line">        <span class="comment">// 格式：数据类型 变量名称；</span></span><br><span class="line">        <span class="keyword">int</span> num1;</span><br><span class="line">        <span class="comment">// 向变量中存入一个数据</span></span><br><span class="line">        <span class="comment">// 格式：变量名称 = 数据值;</span></span><br><span class="line">        num1 = <span class="number">10</span>;</span><br><span class="line">        System.out.println(num1); <span class="comment">// 10</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 改变变量中本来的数字，变成新的数字</span></span><br><span class="line">        num1 = <span class="number">20</span>;</span><br><span class="line">        System.out.println(num1); <span class="comment">// 20</span></span><br><span class="line">        </span><br><span class="line">        <span class="comment">// 使用一步到位的格式来定义变量</span></span><br><span class="line">        <span class="comment">// 格式：数据类型 变量名称 = 数据值;</span></span><br><span class="line">        <span class="keyword">int</span> num2 = <span class="number">25</span>;</span><br><span class="line">        System.out.println(num2); <span class="comment">// 25</span></span><br><span class="line">        </span><br><span class="line">        num2 = <span class="number">35</span>;</span><br><span class="line">        System.out.println(num2);</span><br><span class="line">        System.out.println(<span class="string">&quot;=================&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">byte</span> num3 = <span class="number">30</span>; <span class="comment">//注意：右侧数值范围不能超过左侧数据类型的取值范围</span></span><br><span class="line">        System.out.println(num3); <span class="comment">// 30</span></span><br><span class="line">        <span class="comment">//byte num4 = 400; // 右侧超出了byte数据范围，错误</span></span><br><span class="line">        </span><br><span class="line">        <span class="comment">// 短整型</span></span><br><span class="line">        <span class="keyword">short</span> num5 = <span class="number">50</span>;</span><br><span class="line">        System.out.println(num5);  <span class="comment">// 50</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 长整型</span></span><br><span class="line">        <span class="keyword">long</span> num6 = <span class="number">3000000000L</span>;</span><br><span class="line">        System.out.println(num6); <span class="comment">// 3000000000</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 浮点型</span></span><br><span class="line">        <span class="keyword">float</span> num7 = <span class="number">2.5F</span>;</span><br><span class="line">        System.out.println(num7); <span class="comment">//2.5</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 双精度浮点型</span></span><br><span class="line">        <span class="keyword">double</span> num8 = <span class="number">1.2</span>;</span><br><span class="line">        System.out.println(num8); <span class="comment">//1.2</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 字符型</span></span><br><span class="line">        <span class="keyword">char</span> chara1 = <span class="string">&#x27;A&#x27;</span>;</span><br><span class="line">        System.out.println(chara1); <span class="comment">// A</span></span><br><span class="line">        chara1 = <span class="string">&#x27;中&#x27;</span>;</span><br><span class="line">        System.out.println(chara1); <span class="comment">// 中</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 布尔型</span></span><br><span class="line">        <span class="keyword">boolean</span> var1 = <span class="keyword">true</span>;</span><br><span class="line">        System.out.println(var1); <span class="comment">// true</span></span><br><span class="line">        var1 = <span class="keyword">false</span>;</span><br><span class="line">        System.out.println(var1); <span class="comment">// false</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 将一个变量的数据内容，赋值交给另一个变量</span></span><br><span class="line">        <span class="comment">// 右侧的变量名称var1已经存在，里面装的是false布尔值</span></span><br><span class="line">        <span class="comment">// 将右侧变量里面的false值，向左交给var2变量进行储存</span></span><br><span class="line">        <span class="keyword">boolean</span> var2 = var1;</span><br><span class="line">        System.out.println(var2); <span class="comment">// false</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>变量使用注意事项：</strong></p>
<ol>
<li>如果创建多个变量，那么变量之间的名称不可以重复；</li>
<li>对于float和long类型来说，字母后缀F和L不能丢掉；</li>
<li>如果使用byte或者short类型的变量，那么右侧的数据值不能超过左侧类型的范围；</li>
<li>没有进行赋值的变量不能直接使用，一定要赋值后才能使用；</li>
<li>变量使用不能超过作用域的范围。</li>
</ol>
<p><strong>【作用域】</strong> 从定义变量的一行开始，一直到直接所属的大括号结束为止。</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">哲铭</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://jimmyliang-lzm.github.io/2021/02/09/Java-Note-01/">https://jimmyliang-lzm.github.io/2021/02/09/Java-Note-01/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://jimmyliang-lzm.github.io" target="_blank">Jimmy's TechBlog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E7%AC%94%E8%AE%B0/">笔记</a></div><div class="post_share"><div class="social-share" data-image="/source_storage/java.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2021/08/31/mecanum-kinematics/"><img class="prev-cover" src= "" data-lazy-src="/source_storage/mecan_ka.gif" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">【学术】4-麦克纳姆轮平台运动学建模</div></div></a></div><div class="next-post pull-right"><a href="/2021/01/31/PID-DCMotor-Simulate/"><img class="next-cover" src= "" data-lazy-src="/source_storage/Proteus.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">【学习手记】基于AT89C52单片机的PID直流电机控制系统仿真</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src= "" data-lazy-src="/img/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">哲铭</div><div class="author-info__description">哲铭的个人站点，分享技术学习心得，项目开发心路历程，以及日常沙雕操作，欢迎访问！</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">10</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">22</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">5</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/JimmyLiang-lzm" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:mibemail@163.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">本博客网站因资金原因暂时托管到Github与Gitee上，当发工资的时候会搭建自己的服务器哒🤪！<a target="_blank" rel="noopener" href="https://zmtechn.gitee.io"><button>Gitee访问地址</button></a><a href="https://jimmyliang-lzm.github.io"><button>Github访问地址</button></a></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Java%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%EF%BC%881%EF%BC%89"><span class="toc-number">1.</span> <span class="toc-text">Java学习笔记（1）</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-1-%E4%BB%80%E4%B9%88%E6%98%AFJDK%E3%80%81JRE%E3%80%81JVM"><span class="toc-number">1.1.</span> <span class="toc-text">1.1 什么是JDK、JRE、JVM</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-2-%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE"><span class="toc-number">1.2.</span> <span class="toc-text">1.2 环境配置</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-3-%E7%AC%AC%E4%B8%80%E4%B8%AAJava%E7%A8%8B%E5%BA%8F%EF%BC%9AHelloWorld"><span class="toc-number">1.3.</span> <span class="toc-text">1.3 第一个Java程序：HelloWorld</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-4-Java%E7%A8%8B%E5%BA%8F%E7%9A%84%E6%B3%A8%E9%87%8A"><span class="toc-number">1.4.</span> <span class="toc-text">1.4 Java程序的注释</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-5-%E6%A0%87%E8%AF%86%E7%AC%A6"><span class="toc-number">1.5.</span> <span class="toc-text">1.5 标识符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-6-Java%E5%B8%B8%E9%87%8F"><span class="toc-number">1.6.</span> <span class="toc-text">1.6 Java常量</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-7-%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.7.</span> <span class="toc-text">1.7 基本数据类型</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/10/05/Get_bilibili_cookie/" title="【教程】如何获取B站用户Cookie"><img src= "" data-lazy-src="/source_storage/2233.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="【教程】如何获取B站用户Cookie"/></a><div class="content"><a class="title" href="/2021/10/05/Get_bilibili_cookie/" title="【教程】如何获取B站用户Cookie">【教程】如何获取B站用户Cookie</a><time datetime="2021-10-06T03:00:00.000Z" title="发表于 2021-10-06 11:00:00">2021-10-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/08/31/mecanum-kinematics/" title="【学术】4-麦克纳姆轮平台运动学建模"><img src= "" data-lazy-src="/source_storage/mecan_ka.gif" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="【学术】4-麦克纳姆轮平台运动学建模"/></a><div class="content"><a class="title" href="/2021/08/31/mecanum-kinematics/" title="【学术】4-麦克纳姆轮平台运动学建模">【学术】4-麦克纳姆轮平台运动学建模</a><time datetime="2021-09-01T03:00:00.000Z" title="发表于 2021-09-01 11:00:00">2021-09-01</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/02/09/Java-Note-01/" title="【Java学习】Java入门笔记1"><img src= "" data-lazy-src="/source_storage/java.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="【Java学习】Java入门笔记1"/></a><div class="content"><a class="title" href="/2021/02/09/Java-Note-01/" title="【Java学习】Java入门笔记1">【Java学习】Java入门笔记1</a><time datetime="2021-02-10T04:06:00.000Z" title="发表于 2021-02-10 12:06:00">2021-02-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/31/PID-DCMotor-Simulate/" title="【学习手记】基于AT89C52单片机的PID直流电机控制系统仿真"><img src= "" data-lazy-src="/source_storage/Proteus.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="【学习手记】基于AT89C52单片机的PID直流电机控制系统仿真"/></a><div class="content"><a class="title" href="/2021/01/31/PID-DCMotor-Simulate/" title="【学习手记】基于AT89C52单片机的PID直流电机控制系统仿真">【学习手记】基于AT89C52单片机的PID直流电机控制系统仿真</a><time datetime="2021-01-31T12:50:00.000Z" title="发表于 2021-01-31 20:50:00">2021-01-31</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/29/Matlab_Robot_simulation/" title="【学习手记】MATLAB与机器人仿真项目训练"><img src= "" data-lazy-src="/source_storage/matlabrobotsimulate.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="【学习手记】MATLAB与机器人仿真项目训练"/></a><div class="content"><a class="title" href="/2021/01/29/Matlab_Robot_simulation/" title="【学习手记】MATLAB与机器人仿真项目训练">【学习手记】MATLAB与机器人仿真项目训练</a><time datetime="2021-01-30T05:14:00.000Z" title="发表于 2021-01-30 13:14:00">2021-01-30</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2021 By 哲铭</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>if (!window.MathJax) {
  window.MathJax = {
    tex: {
      inlineMath: [ ['$','$'], ["\\(","\\)"]],
      tags: 'ams'
    },
    chtml: {
      scale: 1.2
    },
    options: {
      renderActions: {
        findScript: [10, doc => {
          for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
            const display = !!node.type.match(/; *mode=display/)
            const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
            const text = document.createTextNode('')
            node.parentNode.replaceChild(text, node)
            math.start = {node: text, delim: '', n: 0}
            math.end = {node: text, delim: '', n: 0}
            doc.math.push(math)
          }
        }, ''],
        insertScript: [200, () => {
          document.querySelectorAll('mjx-container:not\([display]\)').forEach(node => {
            const target = node.parentNode
            if (target.nodeName.toLowerCase() === 'li') {
              target.parentNode.classList.add('has-jax')
            } else {
              target.classList.add('has-jax')
            }
          });
        }, '', false]
      }
    }
  }
  
  const script = document.createElement('script')
  script.src = 'https://cdn.jsdelivr.net/npm/mathjax/MathJax.js?config=TeX-AMS-MML_HTMLorMML'
  script.id = 'MathJax-script'
  script.async = true
  document.head.appendChild(script)
} else {
  MathJax.startup.document.state(0)
  MathJax.texReset()
  MathJax.typeset()
}</script></div><script defer="defer" id="ribbon" src="/js/third-party/canvas-ribbon.js" size="150" alpha="0.6" zIndex="-1" mobile="true" data-click="true"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>